package org.example.Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2022-03-21
 * Time:20:44
 */
public class Demo9 {
    private static boolean isQuit = false;
    private static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        java.lang.Thread t = new java.lang.Thread(() -> {
            while (!isQuit){
                count++;
                System.out.println("hello thread");
                try {
                    java.lang.Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        //只要把isQuit设为True,此时这个循环就退出了,进一步的run就执行完了,再进一步就是线程执行结束了
        try {
            java.lang.Thread.sleep(5000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        //main线程在此时执行isQuit = true的操作，此时t线程的while循环条件不成立，那么t线程就终止了
        isQuit = true;

        for (int i = 0; i <10; i++){
            Thread.sleep(100);
            System.out.println(count);
        }
        System.out.println("终止T线程");
    }
}
